package org.shopen.puremvc.model
{
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	import org.shopen.puremvc.model.domain.testing.Testing;
	
	public class TestingProxy extends Proxy
	{
		public static const NAME : String = "TestingProxy";
		
		public static const TESTINGS_INITIALIZED : String = NAME + ".TESTINGS_INITIALIZED";
		
		public function TestingProxy(data:Object=null)
		{
			super(TestingProxy.NAME);
			
			var xmlData : XML = data as XML;
			if (xmlData != null)
			{
				initData(xmlData);
			}
		}
		
		override public function getProxyName():String
		{
			return TestingProxy.NAME;
		}
		
		public function initData(node : XML) : void
		{
			var result : ArrayCollection = new ArrayCollection();
			var testingNodes : XMLList = node.testing as XMLList;
			for each (var testingNode : XML in testingNodes)
			{
				var testing : Testing = new Testing(testingNode);
				result.addItem(testing);
			}
			setData(result);
			sendResult();
		}
		
		public function get testings() : ArrayCollection
		{
			var result : ArrayCollection = getData() as ArrayCollection; 
			return result;
		}
		
		public function getTesting(testingId : String) : Testing
		{
			var result : Testing = null;
			for each(var testing : Testing in this.testings)
			{
				if (testing.id == testingId)
				{
					result = testing;
					break;
				}
				else
				{
					continue;
				}
			}
			return result;
		}
		
		public function getRandomTesting(testingId : String = null) : Testing
		{
			var result : Testing = null;
			var testing : Testing;
			if (testingId != null)
			{
				testing = getTesting(testingId);
				result = testing.getRandomTesting();
			}
			else
			{
				testing = currentTesting.clone();
				result = testing.getRandomTesting();
			}
			
			return result;
		}
		
		[Bindable]
		private var _currentTesting : Testing = null;
		[Bindable]
		public function set currentTesting(value : Testing) : void
		{
			_currentTesting = value;
		}
		public function get currentTesting() : Testing
		{
			return _currentTesting;
		}
		
		public function getCurrentTesting() : Testing
		{
			var result : Testing = currentTesting;
			return result;
		}
		
		protected function sendResult() : void
		{
			sendNotification(TestingProxy.TESTINGS_INITIALIZED, this.testings);
		}
		
	}
}